/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EscolheFinView.java
 *
 * Created on 10/11/2009, 16:07:12
 */

package view.execucao;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import model.execucao.Formulario;
import model.execucao.ProcAtivDao;
import model.execucao.Transicao;

/**
 *
 * @author Administrador
 */
public class EscolheFinView extends javax.swing.JDialog {

    /** Creates new form EscolheFinView */
    public EscolheFinView(java.awt.Frame parent, boolean modal, Transicao trans) {
        super(parent, modal);
        initComponents();
        this.trans = trans;
        try{
            String desc = new ProcAtivDao().preencheArvoreFinalizacao(trans, jtrfinalizacoes);
            jTextArea1.setText(desc);
            int modo = TreeSelectionModel.SINGLE_TREE_SELECTION;
            TreeSelectionModel tsm = jtrfinalizacoes.getSelectionModel();
            tsm.setSelectionMode(modo);
            }catch(SQLException sql)
            {
                JOptionPane.showMessageDialog(null, sql.getMessage());
            }
        

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtrfinalizacoes = new javax.swing.JTree();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jldesc = new javax.swing.JLabel();
        jbtok = new javax.swing.JButton();
        jbtcancel = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Atividade");
        setResizable(false);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        jtrfinalizacoes.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jtrfinalizacoes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtrfinalizacoesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtrfinalizacoes);

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jTextArea1.setText(" ");
        jTextArea1.setToolTipText("");
        jScrollPane2.setViewportView(jTextArea1);

        jldesc.setText("Descrição :");

        jbtok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/header_complete.gif"))); // NOI18N
        jbtok.setText("OK");
        jbtok.setPreferredSize(new java.awt.Dimension(85, 25));
        jbtok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtokActionPerformed(evt);
            }
        });

        jbtcancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/delete_edit.gif"))); // NOI18N
        jbtcancel.setText("Cancel");
        jbtcancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtcancelActionPerformed(evt);
            }
        });

        jButton1.setText("Histórico");
        jButton1.setPreferredSize(new java.awt.Dimension(85, 25));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtcancel)
                        .addGap(7, 7, 7)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jldesc)
                .addContainerGap(346, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
                .addContainerGap(10, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jldesc)
                .addGap(1, 1, 1)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtcancel)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-425)/2, (screenSize.height-476)/2, 425, 476);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtokActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtokActionPerformed


        DefaultMutableTreeNode node = (DefaultMutableTreeNode) jtrfinalizacoes.getLastSelectedPathComponent();
        if(node == null) {JOptionPane.showMessageDialog(null, "Não há finalização selecionada, verifique!","Erro",0);return;}
        Object nodeInfo = node.getUserObject();
        if(!node.isRoot())
        {
            String nome = node.toString();
            int chave = Integer.valueOf(nome.substring(1, 4));
            
            try{
                if(new ProcAtivDao().permiteValor(trans, chave) == 1)
                {
                    
                    new ValorView(null, true, this,new ProcAtivDao().retornaPergunta(trans, chave)).setVisible(true);
                }
                new Formulario().encontraFin(trans, chave);
                new ProcAtivDao().gravaTarefa(trans, chave, obs);
                dispose();
            }catch(SQLException ex)
            {
                JOptionPane.showMessageDialog(null, ex.getMessage());
                ex.printStackTrace();
            }
            
        }

    }//GEN-LAST:event_jbtokActionPerformed

    private void jbtcancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtcancelActionPerformed
        dispose();
    }//GEN-LAST:event_jbtcancelActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed




            new HistoricoProcessoView(null, true, trans).setVisible(true);


    }//GEN-LAST:event_jButton1ActionPerformed

    private void jtrfinalizacoesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtrfinalizacoesMouseClicked
        if(evt.getClickCount() == 2)
        {
            jbtok.doClick();
        }
    }//GEN-LAST:event_jtrfinalizacoesMouseClicked

    /**
    * @param args the command line arguments
    */


    public void carregaArvore(ResultSet rs)
    {

        if(rs == null)
        {
            return;
        }
        



    }
public String getObs() {
        return obs;
    }

    public void setObs(String obs) {
        this.obs = obs;
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                EscolheFinView dialog = new EscolheFinView(new javax.swing.JFrame(), true, new Transicao());
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton jbtcancel;
    private javax.swing.JButton jbtok;
    private javax.swing.JLabel jldesc;
    private javax.swing.JTree jtrfinalizacoes;
    // End of variables declaration//GEN-END:variables
    Transicao trans;
    private String obs = null;


}
